<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin;
use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Validation\Rule;

class AdminController extends Controller
{


    /**
     * 后台用户
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function  index()
    {



        return view('admin/admin/index');

    }

    /**
     * 编辑
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function  edit(Request $request)
    {
        $params = $request->only(['admin_id']);
        $validator = \Validator::make($params, [
            'admin_id' => 'required|exists:admin,id',

        ], [
            'admin_id.*' => '参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $admin =  Admin::find($request->admin_id);
        $role  =  Role::get();
        return view('admin/admin/edit',['data'=>$admin,'role'=>$role]);
    }

    /**
     * 编辑
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function  add()
    {

        $data['role']=Role::get();
//        $data['users'] = User::get();
        return view('admin/admin/add',$data);
    }




    /**
     * 后台用户列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function data(Request $request)
    {
        $searchParams = json_decode($request->searchParams, true);
        $data = Admin::Name($searchParams['username'])->select('admin.*','roles.name')->join('roles','roles.id','=','admin.role_id')->paginate()->toArray();
        return $this->success(['data'=>$data['data'], 'count'=> $data['total'],'code'=>0]);
    }

    /**
     * 添加后台用户
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addAdmin(Request $request)
    {


        $params = $request->only(['username', 'password',
//            'user_id',
            'role_id']);
        $validator = \Validator::make($params, [
            'username' => 'required|string|max:20|unique:admin',
            'password' => 'required|string|max:20',
            'role_id' => 'required|exists:roles,id',
        ], [
            'username.required' => '账号必填',
            'username.max' => '账号最大长度为20个',
            'username.unique' => '账号已存在',
            'role_id.*' => '角色参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $data=[
            'username'=>$request->username,
            'password'=>bcrypt($request->password),
            'user_id'=>$request->user_id??'',
            'role_id'=>$request->role_id,
        ];
        $re = Admin::add($data);
        if($re){
            return  $this->success(['msg'=>'保存成功']);
        }
        return  $this->error('保存失败');
    }

    /**
     * 账号信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function   detail(Request $request)
    {
        $params = $request->only(['admin_id']);
        $validator = \Validator::make($params, [
            'admin_id' => 'required|exists:admin,id',

        ], [
            'admin_id.*' => '参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
       $admin =  Admin::find($request->admin_id);
        
        return $this->success($admin);
        
    }

    /**
     * 用户编辑
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function  saveAdmin(Request $request)
    {
        $params = $request->only(['username', 'password', 'user_id', 'role_id','admin_id']);

        $validator = \Validator::make($params, [
            'admin_id'=>'required|exists:admin,id',
            'username' =>['required','string','max:20', Rule::unique('admin')->whereNull('deleted_at')->ignore($request->admin_id)],
//            'password' => 'required|string|max:20',
//            'user_id' => 'required|exists:users,id',
            'role_id' => 'required|exists:roles,id',
        ], [
            'username.*' => '账号参数错误',
            'admin_id.*' => '修改参数错误',
//            'user_id.*' => '参数错误',
            'role_id.*' => '角色参数错误',
//            'level.*' => '参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $re  = Admin::edit($request->admin_id,$request->all());
        if($re){
            return  $this->success(['msg'=>'保存成功']);
        }
        return  $this->error('保存失败');
    }


    /**
     * 删除用户
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse\
     */
    public function  del(Request $request)
    {
        $admin = Admin::find($request->admin_id);
        $admin->delete();
        if ($admin->trashed()) {
            return  $this->success(['msg'=>'删除成功']);
        }
        return  $this->error('删除失败');
    }
}
